package com.lepu.app.main.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.Const;
import com.app.utils.LoginUtil;
import com.app.utils.MyNetUtil;
import com.app.utils.MyStoreUtil;
import com.app.utils.Setting;
import com.app.utils.UmengEvent;
import com.app.utils.Utils;
import com.core.lib.application.BaseFragment;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.Observer;
import com.core.lib.utils.main.LogUtilBase;
import com.core.lib.utils.main.UmengHelper;
import com.core.lib.widget.CircularImageView;
import com.lepu.app.fun.index.activity.IndexFileActivity;
import com.lepu.app.fun.my.MyCollectActivity;
import com.lepu.app.fun.my.MyFenSiActivity;
import com.lepu.app.fun.my.MyOrderActivity;
import com.lepu.app.fun.my.MyScoreInfoActivity;
import com.lepu.app.fun.my.MySettingActivity;
import com.lepu.app.fun.my.MyTieZiActivity;
import com.lepu.app.fun.my.MyUserInfoActivity;
import com.lepu.app.fun.my.bean.BeanMyInfo;
import com.lepu.app.main.activity.AppBroswerActivity;
import com.lepu.app.main.activity.MainTabActivity;
import com.lepu.app.usercenter.FeedBackMainActivity;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.pasm.R;

public class MainTabFragmentMy extends BaseFragment implements AsyncRequest,
        Observer {

    private View mMainView = null;
    private boolean mInitDataSuccess = false;// 是否加载完数据

    private BeanMyInfo mBeanMyInfo = null;

    @SuppressLint("InflateParams")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mMainView = LayoutInflater.from(getActivity()).inflate(
                R.layout.main_tab_fragment_my, null, false);
        init();
    }

    @Override
    public void onResume() {
        super.onResume();
        UmengHelper.OnPageStart(UmengEvent.page_wode);

        if (mInitDataSuccess) {

            updateLoginState();
            MyNetUtil.getInstance().requestMyInfo(MainTabActivity.getInstance(), this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        UmengHelper.OnPageEnd(UmengEvent.page_wode);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        ViewGroup p = (ViewGroup) mMainView.getParent();
        if (p != null) {
            p.removeAllViewsInLayout();
        }
        return mMainView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            MyNetUtil.getInstance().requestMyInfo(MainTabActivity.getInstance(), this); // 每次进入这个界面都需要更新数据
            if (!mInitDataSuccess) {
                mInitDataSuccess = true;
                // 第一次进入页面获取数据
                updateLoginState();

                // 从网络获取帖子数量等信息
            }
        }
    }

    /**
     * 初始化data
     */
    private void init() {
        //5.0以下关闭硬件加速
        Utils.setLayerTypeOff(mMainView);

        // user
        RelativeLayout userLayout = (RelativeLayout) mMainView
                .findViewById(R.id.userLayout);
        userLayout.setOnClickListener(mOnclClickListener);

        // menu
        LinearLayout tieziLayout = (LinearLayout) mMainView
                .findViewById(R.id.tieziLayout);
        LinearLayout collectLayout = (LinearLayout) mMainView
                .findViewById(R.id.collectLayout);
        LinearLayout guanzhuLayout = (LinearLayout) mMainView
                .findViewById(R.id.guanzhuLayout);
        LinearLayout fensiLayout = (LinearLayout) mMainView
                .findViewById(R.id.fensiLayout);

        tieziLayout.setOnClickListener(mOnclClickListener);
        collectLayout.setOnClickListener(mOnclClickListener);
        guanzhuLayout.setOnClickListener(mOnclClickListener);
        fensiLayout.setOnClickListener(mOnclClickListener);

        // personal
        RelativeLayout scoreInfoLayout = (RelativeLayout) mMainView
                .findViewById(R.id.scoreInfoLayout);
        RelativeLayout scoreStoreLayout = (RelativeLayout) mMainView
                .findViewById(R.id.scoreStoreLayout);
        RelativeLayout youHuiQuanLayout = (RelativeLayout) mMainView
                .findViewById(R.id.youHuiQuanLayout);
        RelativeLayout feedbackLayout = (RelativeLayout) mMainView
                .findViewById(R.id.feedbackLayout);
        RelativeLayout helpLayout = (RelativeLayout) mMainView
                .findViewById(R.id.helpLayout);
        RelativeLayout settingLayout = (RelativeLayout) mMainView
                .findViewById(R.id.settingLayout);
        RelativeLayout myOrderLayout = (RelativeLayout) mMainView
                .findViewById(R.id.myOrderLayout);
        RelativeLayout reportLayout = (RelativeLayout) mMainView
        		.findViewById(R.id.reportLayout);

        scoreInfoLayout.setOnClickListener(mOnclClickListener);
        scoreStoreLayout.setOnClickListener(mOnclClickListener);
        youHuiQuanLayout.setOnClickListener(mOnclClickListener);
        feedbackLayout.setOnClickListener(mOnclClickListener);
        helpLayout.setOnClickListener(mOnclClickListener);
        settingLayout.setOnClickListener(mOnclClickListener);
        myOrderLayout.setOnClickListener(mOnclClickListener);
        reportLayout.setOnClickListener(mOnclClickListener);

    }

    /**
     * 更新状态信息
     */
    public void updateLoginState() {
        if(mMainView == null)
        {
            return;
        }
        CircularImageView accountIconImageView = (CircularImageView) mMainView
                .findViewById(R.id.accountIconImageView);
        TextView usernameTextView = (TextView) mMainView
                .findViewById(R.id.usernameTextView);

        TextView userWhatUpTextView = (TextView) mMainView
                .findViewById(R.id.userWhatUpTextView);
        TextView rightEditTextView = (TextView) mMainView
                .findViewById(R.id.rightEditTextView);

        if (LoginUtil.checkIsLogin()) {

            LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
            LoginUtil.getUserIcon(getActivity(), accountIconImageView,
                    loginInfo.UserID, true);

            usernameTextView.setText(loginInfo.NickName);

            userWhatUpTextView.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(loginInfo.Whatsup)) {
                userWhatUpTextView.setText(loginInfo.Whatsup);
            } else {
                userWhatUpTextView.setText("修改个性签名");
            }

            rightEditTextView.setVisibility(View.VISIBLE);

        } else {
            usernameTextView.setText("未登录");
            accountIconImageView
                    .setImageResource(R.drawable.my_icon_head_nologin);

            userWhatUpTextView.setVisibility(View.GONE);
            mBeanMyInfo = null;

            rightEditTextView.setVisibility(View.GONE);
        }

        updateInfo();
    }

    private void updateInfo() {
        TextView tieziCountTextView = (TextView) mMainView
                .findViewById(R.id.tieziCountTextView);
        TextView collectCountTextView = (TextView) mMainView
                .findViewById(R.id.collectCountTextView);
        TextView guanzhuCountTextView = (TextView) mMainView
                .findViewById(R.id.guanzhuCountTextView);
        TextView fensiCountTextView = (TextView) mMainView
                .findViewById(R.id.fensiCountTextView);
        TextView scoreCountTextView = (TextView) mMainView
                .findViewById(R.id.scoreCountTextView);

        if (mBeanMyInfo != null) {

            tieziCountTextView.setText(mBeanMyInfo.ThreadCount);
            collectCountTextView.setText(mBeanMyInfo.CollectCount);
            guanzhuCountTextView.setText(mBeanMyInfo.FollowCount);
            fensiCountTextView.setText(mBeanMyInfo.FansCount);
            //scoreCountTextView.setText(mBeanMyInfo.AvailableCredits + "");
            scoreCountTextView.setText("");
        } else {
            tieziCountTextView.setText("0");
            collectCountTextView.setText("0");
            guanzhuCountTextView.setText("0");
            fensiCountTextView.setText("0");
            scoreCountTextView.setText("");
        }
    }

    @Override
    public void notify(String name, Object sender, Object data) {

    }

    /**
     * 点击事件
     */
    View.OnClickListener mOnclClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            int rid = view.getId();
            switch (rid) {
                case R.id.userLayout:

                    boolean flagUser = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagUser) {
                        Intent userIntent = new Intent(getActivity(),
                                MyUserInfoActivity.class);
                        MainTabActivity.getInstance().startActivity(userIntent,
                                true);
                    }
                    break;
                case R.id.tieziLayout:

                    boolean flagTiezi = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagTiezi) {
                        Intent tieziIntent = new Intent(getActivity(),
                                MyTieZiActivity.class);
                        MainTabActivity.getInstance().startActivity(tieziIntent,
                                true);
                    }
                    break;
                case R.id.collectLayout:

                    boolean flagCollect = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagCollect) {
                        Intent collectIntent = new Intent(getActivity(),
                                MyCollectActivity.class);
                        MainTabActivity.getInstance().startActivity(collectIntent,
                                true);
                    }
                    break;
                case R.id.guanzhuLayout:

                    boolean flagGuanzhu = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagGuanzhu) {
                        Intent guanzhuIntent = new Intent(getActivity(),
                                MyFenSiActivity.class);
                        guanzhuIntent.putExtra("fensi_guanzhu",
                                MyFenSiActivity.TYPE_GUANZHU);
                        guanzhuIntent.putExtra("TargetUserID", MyApplication
                                .getInstance().getLoginInfo().UserID);
                        MainTabActivity.getInstance().startActivity(guanzhuIntent,
                                true);
                    }
                    break;
                case R.id.fensiLayout:

                    boolean flagFensi = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagFensi) {
                        Intent fensiIntent = new Intent(getActivity(),
                                MyFenSiActivity.class);
                        fensiIntent.putExtra("fensi_guanzhu",
                                MyFenSiActivity.TYPE_FENSI);
                        fensiIntent.putExtra("TargetUserID", MyApplication
                                .getInstance().getLoginInfo().UserID);
                        MainTabActivity.getInstance().startActivity(fensiIntent,
                                true);
                    }
                    break;
                case R.id.scoreInfoLayout:

                    UmengHelper.CustomEvent(getActivity(), UmengEvent.event_wode_wodejifen);

                    boolean flagScore = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagScore) {
                        int score = 0;
                        if(mBeanMyInfo != null)
                        {
                            score = mBeanMyInfo.AvailableCredits;
                        }
                        Intent scoreInfoIntent = new Intent(getActivity(),
                                MyScoreInfoActivity.class);
                        scoreInfoIntent.putExtra("ScoreNumber",
                                score);
                        MainTabActivity.getInstance().startActivity(
                                scoreInfoIntent, true);
                    }
                    break;
                case R.id.scoreStoreLayout:

                    UmengHelper.CustomEvent(getActivity(), UmengEvent.event_wode_jifenhuanli);

                    MyStoreUtil.getInstance().requestMoneyMarket(
                            MainTabActivity.getInstance(), "");
                    break;
                case R.id.youHuiQuanLayout:

                    UmengHelper.CustomEvent(getActivity(), UmengEvent.event_wode_youhuijuan);

                    boolean flagYouHuiQuan = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagYouHuiQuan) {
                        Intent youHuiQuanIntent = new Intent(getActivity(),
                                AppBroswerActivity.class);
                        youHuiQuanIntent.putExtra("url",
                                Setting.getMyYouHuiQuanUrl(LoginUtil.getUserId()));
                        //youHuiQuanIntent.putExtra("title", "我的优惠券");
                        youHuiQuanIntent.putExtra("from_type",
                                Const.AppBroswerType.TYPE_MY_YOU_HUI_QUAN);
                        MainTabActivity.getInstance().startActivity(
                                youHuiQuanIntent, true);
                    }
                    break;
                case R.id.feedbackLayout:

                    UmengHelper.CustomEvent(getActivity(), UmengEvent.event_wode_yijianfankui);

                    boolean flagFeedback = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagFeedback) {
                        Intent feedbackIntent = new Intent(getActivity(),
                                FeedBackMainActivity.class);
                        MainTabActivity.getInstance().startActivity(feedbackIntent,
                                true);
                    }
                    break;
                case R.id.helpLayout:

                    Intent helpIntent = new Intent(getActivity(),
                            AppBroswerActivity.class);
                    helpIntent.putExtra("url", Setting.getMyHelpUrl());
                    helpIntent.putExtra("title", "使用帮助");
                    helpIntent.putExtra("need_share", false);
                    MainTabActivity.getInstance().startActivity(helpIntent, true);
                    break;
                case R.id.settingLayout:

                    Intent settingIntent = new Intent(getActivity(),
                            MySettingActivity.class);
                    MainTabActivity.getInstance()
                            .startActivity(settingIntent, true);
                    break;
                case R.id.myOrderLayout:

                    UmengHelper.CustomEvent(getActivity(), UmengEvent.event_wode_wodedingdan);

                    boolean flagMyOrder = LoginUtil
                            .jumpLogin((BaseFragmentActivity) getActivity());
                    if (!flagMyOrder) {
                        Intent orderIntent = new Intent(getActivity(),
                                MyOrderActivity.class);
                        MainTabActivity.getInstance()
                                .startActivity(orderIntent, true);
                    }
                    break;
                case R.id.reportLayout:

                    UmengHelper.CustomEvent(getActivity(), UmengEvent.event_wode_jianchabaogao);

					boolean flagReport = LoginUtil.jumpLogin((BaseFragmentActivity) getActivity());
					if (!flagReport) {
						Intent reportIntent = new Intent(getActivity(),IndexFileActivity.class);
						reportIntent.putExtra("index", 1);
						MainTabActivity.getInstance().startActivity(reportIntent,true);
					}
                	break;
                default:
                    break;
            }
        }
    };

    @Override
    public void RequestComplete(Object object, Object data) {
        if (object.equals(MyNetUtil.REQUEST_MY_INFO)) {
            LogUtilBase.LogD(null, "REQUEST_MY_INFO success");

            mBeanMyInfo = BeanMyInfo.parseDetailInfo((String) data);
            updateInfo();
        }
    }

    @Override
    public void RequestError(Object object, int errorId, String data) {
        if (object.equals(MyNetUtil.REQUEST_MY_INFO)) {
            LogUtilBase.LogD(null, "REQUEST_MY_INFO fail");
        }
    }

}
